package com.example.pharmacypick.service.impl;

import com.example.pharmacypick.dto.DrugDto;
import com.example.pharmacypick.dto.RecordDto;
import com.example.pharmacypick.mapper.RecordMapper;
import com.example.pharmacypick.pojo.Record;
import com.example.pharmacypick.service.RecordService;
import com.example.pharmacypick.vo.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    RecordMapper recordMapper;

    @Override
    public PageInfo<Record> findALlRecords(RecordDto recordDto) {
        try (Page<Object> pages = PageHelper.startPage(recordDto.getPageNum(), recordDto.getPageSize())){

            List<Record> allRecords = recordMapper.findAllRecords(recordDto);

            return new PageInfo<>(pages.getTotal(), allRecords);
        }
    }

    @Override
    public void addRecordByPrescription(int prescriptionId) {
        //1.首先查出record对象
        Record recordByPrescription = recordMapper.findRecordByPrescription(prescriptionId);

        //2.插入record记录
        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        recordByPrescription.setRecordTime(new Date());

        recordMapper.addRecord(recordByPrescription);

    }


}
